package com.litidea.commons.utils;

import javax.mail.internet.MimeMessage;

import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

public class MailUtils {
	private static String MAIL_DEFAULT_FROM = (String)PropertiesGetter.getContextProperty("mail.from");

	private static String MAIL_DEFAULT_CC = null;

	private static String MAIL_DEFAULT_BCC = null;

	/**
	 * 发送邮件
	 * 
	 * @param mailSender
	 * @param sendTo
	 * @param sendCc
	 * @param sendBcc
	 * @param subject
	 * @param text
	 */
	public static void sendMail(JavaMailSender mailSender, String sendTo,
			String sendCc, String sendBcc, String subject, String text) {
		/*// build message
		SimpleMailMessage mailMessage = new SimpleMailMessage();
		mailMessage.setFrom(MAIL_DEFAULT_FROM);
		mailMessage.setTo(sendTo);
		if(sendCc != null) mailMessage.setCc(sendCc);
		if(sendBcc != null) mailMessage.setBcc(sendBcc);
		mailMessage.setSubject(subject);
		mailMessage.setText(text);
		// send mail
		mailSender.send(mailMessage);*/
		
		MimeMessage mime = mailSender.createMimeMessage();
		MimeMessageHelper helper;
		try{
			helper = new MimeMessageHelper(mime, true, "utf-8");
			helper.setFrom(MAIL_DEFAULT_FROM);
			helper.setTo(sendTo);
			if(sendCc != null) helper.setCc(sendCc);
			if(sendBcc != null) helper.setBcc(sendBcc);
			helper.setSubject(subject);
			helper.setText(text);
		}catch(Exception e){
			
		}
		mailSender.send(mime);
	}

	/**
	 * 没有CC和BCC
	 * @param mailSender
	 * @param sendTo
	 * @param subject
	 * @param text
	 */
	public static void sendMail(JavaMailSender mailSender, String sendTo,
			String subject, String text) {
		sendMail(mailSender, sendTo, MAIL_DEFAULT_CC, MAIL_DEFAULT_BCC,
				subject, text);
	}
}
